
//新增采购单
import {isEmpty, isNotEmpty} from "../../../js/commonUtils";
import tiyData from "./orderImportProduct_data";


export const addOrderPurchase = function (self, tiyData, name) {
  if(tiyData.tData.orderPurchase.type==1){
    tiyData.tStatus.modalAdd_cai.isLoading = true;
  }
  if(tiyData.tData.orderPurchase.type==2){
    tiyData.tStatus.modalAdd_tui.isLoading = true;
  }
  if(tiyData.tData.orderPurchase.type==3){
    tiyData.tStatus.modalAdd_bu.isLoading = true;
  }
  if(tiyData.tData.orderPurchase.type==4){
    tiyData.tStatus.modalAdd_huan.isLoading = true;
  }

  self.$refs[name].validate((valid) => {
    if (valid) {
      if(isNotEmpty(tiyData.tData.orderPurchase.purchaseNo)){
        if(tiyData.tData.orderPurchase.type==1){
          if(tiyData.tData.orderPurchase.purchaseAmount==0||isEmpty(tiyData.tData.orderPurchase.purchaseAmount)){
            self.$Message.error('请输入采购金额');
            changeModelLoading(self, tiyData);
            return;
          }
        }
        if(tiyData.tData.orderPurchase.type==2){
          if(tiyData.tData.orderPurchase.purchaseAmount==0||isEmpty(tiyData.tData.orderPurchase.purchaseAmount)){
            self.$Message.error('请输入退货金额');
            changeModelLoading(self, tiyData);
            return;
          }
        }
      }

      self.$apiNewOrder.addOrderPurchase(tiyData.tData.orderPurchase).then(response => {
        changeModelLoading(self, tiyData);
        if (response.success) {
          self.$Message.success("操作成功!");
          tiyData.tStatus.modalAdd_cai.isShow = false;
          tiyData.tStatus.modalAdd_tui.isShow = false;
          tiyData.tStatus.modalAdd_bu.isShow = false;
          tiyData.tStatus.modalAdd_huan.isShow = false;
          self.queryOrderItemList(tiyData.tData.orderPurchase.orderId);
        } else {
          self.$Message.error(response.errorMessage);
        }
      });
    }else {
      changeModelLoading(self, tiyData);
    }
  });
};

//商品加入采购单
export const joinPurchaseOrder = function (self, tiyData, name) {
  let data=[];
  let index=tiyData.tData.orderPurchaseProduct.index;
  if(self.purchaseType!=2){
    tiyData.tData.orderPurchaseProduct.refundInfo={};
  }
  let product={
    id:'',
    orderPurchaseId:tiyData.tData.orderPurchaseProduct.orderPurchaseId,
    productId:tiyData.tData.orderPurchaseProduct.productId,
    productName:tiyData.tData.orderPurchaseProduct.productName,
    productCode:tiyData.tData.orderPurchaseProduct.productCode,
    zipImage:tiyData.tData.orderPurchaseProduct.zipImage,
    createTime:'',
    note:tiyData.tData.orderPurchaseProduct.note,
    purchaseCount:tiyData.tData.orderPurchaseProduct.purchaseCount,
    isAll:tiyData.tData.orderPurchaseProduct.isAll,
    colorList:tiyData.tData.orderPurchaseProduct.colorList,
    refundInfo: tiyData.tData.orderPurchaseProduct.refundInfo,
  }
  data.push(product);
  if(data.length>0){
    let param={
      data:data,
    }
    tiyData.tStatus.joinPurchaseAdd.isLoading = true;
    self.$refs[name].validate((valid) => {
      if (valid) {
        self.$apiNewOrder.addOrderPurchaseProduct(param).then(response => {
          changeModelLoading(self, tiyData);
          if (response.success) {
            self.$Message.success("操作成功!");
            tiyData.tStatus.joinPurchaseAdd.isShow = false;
            self.showNotPurchase=false;
            self.queryOrderItemList(tiyData.tData.orderPurchase.orderId);
          } else {
            self.$Message.error(response.errorMessage);
          }
        });
      }else {
        changeModelLoading(self, tiyData);
      }
    });
  }

};

//添加商品
export const addPurchaseProduct = function (self, tiyData) {
  let data=[];
  self.pendingProductListFilter.forEach(function(item){
    if(item.check){
      let colorList=[];
      let checkLength=0;
      item.newOrderItemList.forEach(function(y){
        if(y.checked){
          checkLength++;
          let color={
            "orderItemId":y.id,
            "color":y.color,
            "sizeName":y.sizeName,
            "amount":y.amount,
          }
          colorList.push(color);
        }
      })
      let isAll=1;
      let temColorList=[];
      if(checkLength!=0&&item.newOrderItemList.length!=checkLength){
        isAll=2;
        temColorList=[...colorList];
      }
      let refundInfo={};
      if(self.purchaseType==2){
        refundInfo={
          refundCount:item.refundCount,
          refundAmount:item.refundAmount,
          refundNo:item.refundNo,
        }
      }

      let product={
        id:'',
        orderPurchaseId:self.orderPurchaseId,
        productId:item.productId,
        productName:item.productName,
        productCode:item.productCode,
        zipImage:item.zipImage,
        createTime:'',
        note:item.note,
        purchaseCount:item.amount,
        isAll:isAll,
        colorList:temColorList,
        refundInfo:refundInfo,
      }
      data.push(product);
    }
  })
  if(data.length>0){
    tiyData.tStatus.productAdd.isLoading = true;
    let param={
      data:data,
    }
    self.$apiNewOrder.addOrderPurchaseProduct(param).then(response => {
      changeModelLoading(self, tiyData);
      if (response.success) {
        self.$Message.success("操作成功!");
        tiyData.tStatus.productAdd.isShow = false;
        self.queryOrderItemList(tiyData.tData.orderPurchase.orderId);
      } else {
        self.$Message.error(response.errorMessage);
      }
    });
  }else{
    tiyData.tStatus.productAdd.isShow=true;
    self.$Message.error("请选择一个商品");
    // changeModelLoading(self, tiyData);
  }

};

export const handleRefundInfo=function (self, tiyData,name){
  tiyData.tStatus.modalRefund.isLoading = true;
  self.$refs[name].validate((valid) => {
    if (valid) {
      changeModelLoading2(self, tiyData);
      self.pendingProductListFilter.forEach(function(product){
        if(product.productId==tiyData.tStatus.modalRefund.productId){
          product.refundCount=tiyData.tStatus.modalRefund.refundInfo.refundCount;
          product.refundAmount=tiyData.tStatus.modalRefund.refundInfo.refundAmount;
          product.refundNo=tiyData.tStatus.modalRefund.refundInfo.refundNo;
        }
      })
      tiyData.tStatus.modalRefund.isShow = false;
    }else{
      changeModelLoading2(self, tiyData);
    }
  });
};

function changeModelLoading2(self, tiyData){
  tiyData.tStatus.modalRefund.isLoading = false;
  self.$nextTick(function () {
    tiyData.tStatus.modalRefund.isLoading = true;
  });
}

function changeModelLoading(self, tiyData) {
  if(tiyData.tData.orderPurchase.type==1){
    tiyData.tStatus.modalAdd_cai.isLoading = false;
  }
  if(tiyData.tData.orderPurchase.type==2){
    tiyData.tStatus.modalAdd_tui.isLoading = false;
  }
  if(tiyData.tData.orderPurchase.type==3){
    tiyData.tStatus.modalAdd_bu.isLoading = false;
  }
  if(tiyData.tData.orderPurchase.type==4){
    tiyData.tStatus.modalAdd_huan.isLoading = false;
  }
  self.$nextTick(function () {
    if(tiyData.tData.orderPurchase.type==1){
      tiyData.tStatus.modalAdd_cai.isLoading = true;
    }
    if(tiyData.tData.orderPurchase.type==2){
      tiyData.tStatus.modalAdd_tui.isLoading = true;
    }
    if(tiyData.tData.orderPurchase.type==3){
      tiyData.tStatus.modalAdd_bu.isLoading = true;
    }
    if(tiyData.tData.orderPurchase.type==4){
      tiyData.tStatus.modalAdd_huan.isLoading = true;
    }
  });

  tiyData.tStatus.joinPurchaseAdd.isLoading = false;
  self.$nextTick(function () {
    tiyData.tStatus.joinPurchaseAdd.isLoading = true;
  });
}
